package com.gmrz.uaf.protocol.v1.processor;

import com.gmrz.uaf.common.GuiceUtil;
import com.gmrz.uaf.db.DAOException;
import com.gmrz.uaf.db.DAOFactory;
import com.gmrz.uaf.db.UAFDAOFactory;
import com.gmrz.uaf.db.UAFDBConnectionMgr;
import com.gmrz.uaf.db.dao.DevicesDao;
import com.gmrz.uaf.protocol.v1.processor.exception.PolicyNotFoundException;
import com.gmrz.uaf.protocol.v1.processor.exception.UAFErrorCode;
import com.gmrz.uaf.protocol.v1.schema.*;
import com.gmrz.util.db.DBUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chentaikun on 16/12/19.
 */
public class UAFDeviceProcessor extends UAFBaseProcessor {

    private static final Logger LOG = LogManager.getLogger(UAFDeviceProcessor.class);
    public List<DeviceDetails> list(String userName, String appID) throws DAOException{

        Connection conn = null;
        List<DeviceDetails> list;
        try {
            conn = UAFDBConnectionMgr.getConnection(true);
//            DevicesDao devicesDao = GuiceUtil.getProcessorInjector().getInstance(DAOFactory.class)
//                    .createDevicesDAO(conn);
            DevicesDao devicesDao = UAFDAOFactory.createDevicesDAO(conn);
            list =  devicesDao.forUserNameAndAppID(userName,appID);

            return list;
        }catch (SQLException e){
            LOG.error("Failed to access T_DEVICES data, ", e);
            throw new DAOException(UAFErrorCode.DB_DATABASE_CONNECTION_ERROR, e);
        }finally {
            DBUtil.close(conn);
        }
    }

    public List<RegistrationRequest> support(String userName,String authType,String appID,String transType) throws PolicyNotFoundException{
        RegistrationRequest request = GuiceUtil.getProcessorInjector()
                .getInstance(RegistrationRequest.class);
        byte[] challenge = getRandom(32);
        Policy p = this.policyManager.getNamedPolicy(authType,appID,transType);
        LOG.debug("getNamedPolicy authType:"+authType+",appID:"+appID+",transType:"+transType+",policy:"+p);
        if (p == null) {
            LOG.error("Requested policy {} is not available on server", authType);
            throw new PolicyNotFoundException(UAFErrorCode.BIZ_POLICY_NOT_FOUND);
        }
        request.getHeader().getServerData().withChallenge(challenge)
                .withPolicyName(p.getPolicyName()).withUserName(userName);
        request.getHeader().setApplicationID(this.getServerConfig().getApplicationID(appID,transType));
        request.getChallenge().setChallenge(challenge);
        request.setPolicy(p);
        request.setUsername(userName);
        // 查询当前的应用id、业务类型是否已经被放到了redis缓存中了，是拒绝访问
        if(serverConfig.getAppIdUrlBoolean(appID,transType)) {
            setFacetsAaid(request, appID);
        }
        List<RegistrationRequest> requests = new ArrayList<RegistrationRequest>();
        requests.add(request);
        return requests;
    }
}
